package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1、判断当前加入购物车的商品是否已经存在了
        /*1.2、*/
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        /*1.3、dto中并没有登录用户id：userId，*/
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        /*1.1、将购物车数据查询出来*/
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        /*虽然返回的是list集合，但不可能查出多个购物车数据，按当前条件查询只有两种结果：1、查不到 2、查到1条数据*/
        //2、如果有，则将数量加一
        if (list != null && list.size() > 0) {
            /*2.1、有的话取出来那1条数据，并且数量加1*/
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            /*2.2、执行更新操作*/
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //3、如果没有，则插入一条商品数据到数据库
            //3.1、判断本次添加到购物车的是套餐还是菜品
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //3.2、本次添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

                /*shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());*/
            }else{
                //3.2、本次添加到购物车的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

               /* shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());*/
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //4、插入数据库
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /** 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        /*当前查询只需要传一个userId进来就行，因为是查询某个用户的购物车数据*/
        //1、获得当前微信用户的id（userId）
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }
    /**
     * 清空购物车商品
     */
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        /*1、要删除购物车中的数据，首先要查询出来购物车中的商品*/
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0) {
            //2、购物车不是空的，得到购物车的数据
            shoppingCart = list.get(0);

            //3、判断购物车中的商品数量
            Integer number = shoppingCart.getNumber();
            /*如果商品数量是1，直接根据id删除购物车数据*/
            if (number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }

    }
}
